package best.driver.android.common.network;

import android.content.Context;
import best.driver.android.volley.DefaultRetryPolicy;
import best.driver.android.volley.RequestQueue;
import best.driver.android.volley.toolbox.JsonObjectRequest;
import best.driver.android.volley.toolbox.Volley;

public class NetworkManager implements INetworkManager {

    public static final String REQUEST_TAG = "EDJ_API_REQUEST_TAG";

    private RequestQueue mRequestQueue;

    public NetworkManager(Context ctx) {
        mRequestQueue = Volley.newRequestQueue(ctx);
    }

    public RequestQueue getRequestQueue() {
        return mRequestQueue;
    }

    @Override
    public void enqueueRequest(ApiRequest request) {
//        Logger.d("NETWORK", request.getUrl());
        JsonObjectRequest volleyRequest = request.getRequest();
        volleyRequest.setTag(request);
        volleyRequest.setRetryPolicy(new DefaultRetryPolicy(20000/*AppProxyFactory.getProxy().getAppConfig().getHttpTimeout() * 1000*/, 0, 0));
        mRequestQueue.add(volleyRequest);
    }

    @Override
    public void cancelAllRequest() {
        mRequestQueue.cancelAll(REQUEST_TAG);
    }

}
